import 'package:hive/hive.dart';
import '../../domain/entities/favorite.dart';
import '../../domain/entities/anime.dart';
import 'anime_model.dart';

part 'favorite_model.g.dart';

/// 收藏数据模型
/// 用于Hive本地存储
@HiveType(typeId: 1)
class FavoriteModel extends HiveObject {
  @HiveField(0)
  final String id;

  @HiveField(1)
  final int animeId;

  @HiveField(2)
  final Map<String, dynamic> animeData;

  @HiveField(3)
  final String status;

  @HiveField(4)
  final int addedDateTimestamp;

  @HiveField(5)
  final int? watchedEpisodes;

  @HiveField(6)
  final double? personalScore;

  @HiveField(7)
  final String? notes;

  FavoriteModel({
    required this.id,
    required this.animeId,
    required this.animeData,
    required this.status,
    required this.addedDateTimestamp,
    this.watchedEpisodes,
    this.personalScore,
    this.notes,
  });

  /// 从收藏实体创建模型
  factory FavoriteModel.fromEntity(Favorite favorite) {
    return FavoriteModel(
      id: favorite.id,
      animeId: favorite.animeId,
      animeData: AnimeModel.fromEntity(favorite.anime).toJson(),
      status: favorite.status.key,
      addedDateTimestamp: favorite.addedDate.millisecondsSinceEpoch,
      watchedEpisodes: favorite.watchedEpisodes,
      personalScore: favorite.personalScore,
      notes: favorite.notes,
    );
  }

  /// 转换为收藏实体
  Favorite toEntity() {
    final anime = AnimeModel.fromJson(animeData).toEntity();
    return Favorite(
      id: id,
      animeId: animeId,
      anime: anime,
      status: FavoriteStatusExtension.fromKey(status),
      addedDate: DateTime.fromMillisecondsSinceEpoch(addedDateTimestamp),
      watchedEpisodes: watchedEpisodes,
      personalScore: personalScore,
      notes: notes,
    );
  }

  /// 更新收藏状态
  FavoriteModel updateStatus(FavoriteStatus newStatus) {
    return FavoriteModel(
      id: id,
      animeId: animeId,
      animeData: animeData,
      status: newStatus.key,
      addedDateTimestamp: addedDateTimestamp,
      watchedEpisodes: watchedEpisodes,
      personalScore: personalScore,
      notes: notes,
    );
  }

  /// 更新观看进度
  FavoriteModel updateWatchedEpisodes(int newWatchedEpisodes) {
    return FavoriteModel(
      id: id,
      animeId: animeId,
      animeData: animeData,
      status: status,
      addedDateTimestamp: addedDateTimestamp,
      watchedEpisodes: newWatchedEpisodes,
      personalScore: personalScore,
      notes: notes,
    );
  }

  /// 更新个人评分
  FavoriteModel updatePersonalScore(double newScore) {
    return FavoriteModel(
      id: id,
      animeId: animeId,
      animeData: animeData,
      status: status,
      addedDateTimestamp: addedDateTimestamp,
      watchedEpisodes: watchedEpisodes,
      personalScore: newScore,
      notes: notes,
    );
  }

  /// 更新备注
  FavoriteModel updateNotes(String newNotes) {
    return FavoriteModel(
      id: id,
      animeId: animeId,
      animeData: animeData,
      status: status,
      addedDateTimestamp: addedDateTimestamp,
      watchedEpisodes: watchedEpisodes,
      personalScore: personalScore,
      notes: newNotes,
    );
  }
}

/// AnimeModel 扩展
extension AnimeModelExtension on AnimeModel {
  /// 从实体创建模型
  static AnimeModel fromEntity(Anime anime) {
    return AnimeModel(
      id: anime.id,
      title: anime.title,
      titleJapanese: anime.titleJapanese,
      titleEnglish: anime.titleEnglish,
      titleSynonyms: anime.titleSynonyms,
      imageUrl: anime.imageUrl,
      synopsis: anime.synopsis,
      score: anime.score,
      status: anime.status,
      episodes: anime.episodes,
      year: anime.year,
      season: anime.season,
      genres: anime.genres,
      studios: anime.studios,
      trailer: anime.trailer,
      url: anime.url,
      ranked: anime.ranked,
      popularity: anime.popularity,
      members: anime.members,
      favorites: anime.favorites,
      aired: anime.aired,
      rating: anime.rating,
    );
  }
} 